<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Forum Admin Controller
 *
 * @package		SimplyPost
 * @subpackage	Forum
 * @category	Controller
 * @author		Pascal Kriete
 */

class Admin extends Administrator {

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Admin()
	{
		parent::Administrator();
		$this->lang->load('forum');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Has no real function - redirect
	 *
	 * @access	public
	 */
	function index()
	{
		redirect('home/');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Add a new forum
	 *
	 * @access	public
	 * @param	integer	parent id
	 */
	function add($id)
	{
		set_title($this->lang->line('forum_add_title'));
		$this->breadcrumb->from_db($id, 'category', 'Add Forum');

		$data = array('id' => $id);
		
		$this->load->helper('form');
		$this->load->library('validation');

		$rules['desc'] = "trim|required|xss_clean";
		$rules['name'] = "trim|required|xss_clean";
		$this->validation->set_rules($rules);

		$fields['desc'] = 'Description';
		$fields['name'] = 'Forum Title';
		$this->validation->set_fields($fields);

		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');

		if ($this->validation->run() == FALSE)
		{
			$this->load->view('admin/add', $data);
		}
		else
		{
			$this->load->model('For_model', 'Forum');
			$posts = $this->Forum->create($this->input->post('name'), $this->input->post('desc'), $id);
			
			$this->session->set_flashdata('msg', $this->lang->line('forum_added'));
			redirect('home');
		}
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Delete a forum
	 *
	 * @access	public
	 * @param	integer	forum id
	 * @param	integer	parent id
	 */
	function delete($id, $category)
	{
		$this->load->model('For_model', 'Forum');
		$this->Forum->delete($id);
		
		$this->session->set_flashdata('msg', $this->lang->line('forum_deleted'));
		
		if ($_SERVER['HTTP_REFERER'] == base_url())
			redirect('home');
		else if ($_SERVER['HTTP_REFERER'] == base_url().'home')
			redirect('home');
		else
			redirect('category/view/'.$category);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Edit forum title through ajax request
	 *
	 * @access	public
	 * @param	integer	forum id
	 */
	function edit_title($id)
	{
		if (!AJAX)
			show_404();
		
		$this->load->model('For_model', 'Forum');
		$data = $this->Forum->get($id);
				
		$this->load->helper('form');
		$this->load->library('validation');

		$rules['name'] = "trim|required|xss_clean";
		$this->validation->set_rules($rules);

		$fields['name'] = 'Forum Title';
		$this->validation->set_fields($fields);

		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');

		if ($this->validation->run() == FALSE)
		{
			echo 'Invalid Input';
		}
		else
		{
			$this->load->model('For_model', 'Forum');
			$this->Forum->update($id, array('name' => $this->input->post('name')) );

			echo $_POST['name'];
		}		
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Edit forum description through ajax call
	 *
	 * @access	public
	 * @param	integer	forum id
	 */
	function edit_description($id)
	{
		if (!AJAX)
			show_404();
		
		$this->load->model('For_model', 'Forum');
		$data = $this->Forum->get($id);
				
		$this->load->helper('form');
		$this->load->library('validation');

		$rules['desc'] = "trim|required|xss_clean";
		$this->validation->set_rules($rules);

		$fields['desc'] = 'Description';
		$this->validation->set_fields($fields);

		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');

		if ($this->validation->run() == FALSE)
		{
			echo 'Invalid Input';
		}
		else
		{
			$this->load->model('For_model', 'Forum');
			$this->Forum->update($id, array('description' => $this->input->post('desc')) );
			
			echo $_POST['desc'];
		}
	}
}